package me.pplay.playtalk.adapter;

import java.util.ArrayList;
import java.util.List;

import me.pplay.playtalk.ApplicationTrans;
import me.pplay.playtalk.R;
import me.pplay.playtalk.holder.ConversationFriendHolder;
import me.pplay.playtalk.protocol.TpChat;
import me.pplay.playtalk.protocol.bean.TPBuddyinfo;
import me.pplay.playtalk.util.AsyncImageLoader;
import me.pplay.playtalk.util.AsyncImageLoader.ImageCallback;
import me.pplay.playtalk.util.BuddyInfoUtil;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class ConversationFriendAdapter extends BaseAdapter{
	private Context mContext;
	private List<TPBuddyinfo> mList;
	public  List<TPBuddyinfo> selectedList;
	private AsyncImageLoader imageLoader;
	private Handler mHandler;
	private TpChat tpChat;

	public ConversationFriendAdapter(Context context, List<TPBuddyinfo> list, AsyncImageLoader imageLoader, Handler handler){
		mContext			= context;
		mList				= list;
		mHandler			= handler;
		this.imageLoader	= imageLoader;
		selectedList		= new ArrayList<TPBuddyinfo>();
		tpChat				= new ApplicationTrans().getTpChat();
	}
	
	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ConversationFriendHolder holder = null;
		if(convertView == null){
			holder			= new ConversationFriendHolder();
			convertView		= (View)LayoutInflater.from(mContext).inflate(R.layout.conversation_friend_item, null);
			holder.icon		= (ImageView)convertView.findViewById(R.id.icon);
			holder.cbo		= (CheckBox)convertView.findViewById(R.id.cbo);
			holder.title	= (TextView)convertView.findViewById(R.id.title);
			holder.des		= (TextView)convertView.findViewById(R.id.des);
			convertView.setTag(holder);
		}else{
			holder 			= (ConversationFriendHolder)convertView.getTag();
		}
		final TPBuddyinfo buddyinfo = mList.get(position);
		setImageIcon(holder.icon, buddyinfo.getAvatar_fpath(), buddyinfo.getAvatar_sid(),buddyinfo.getAvatarid());
		holder.title.setText(BuddyInfoUtil.getAlias(buddyinfo));
		if(buddyinfo.getSignature().equals(""))
			holder.des.setVisibility(View.GONE);
		else
			holder.des.setText(buddyinfo.getSignature());
		holder.cbo.setChecked(selectedList.contains(buddyinfo));
		holder.cbo.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				CheckBox cbo = (CheckBox)v;
				if(cbo.isChecked()){
					if(selectedList.size() == 4){
						cbo.setChecked(false);
						mHandler.sendEmptyMessage(11);
						return;
					}
					selectedList.add(buddyinfo);
				}else{
					selectedList.remove(buddyinfo);
				}
				mHandler.sendEmptyMessage(8);
			}
		});
		
		return convertView;
	}

	private void setImageIcon(final ImageView imageView, final String  avatar_fpath, int avatar_sid, int avatarid){
		
		if(avatar_fpath.equals("")){
			try {
				imageView.setBackgroundResource(R.drawable.class.getDeclaredField(""+avatarid).getInt(mContext));
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		
		Bitmap bitmap = imageLoader.loadBitmap("http://" + tpChat.tp_chat_gethttpaddr_data(avatar_sid) + "/" + avatar_fpath, new ImageCallback() {
			
			@Override
			public void imageLoaded(Bitmap imageBitmap, String imageUrl) {
				if(imageBitmap != null)
					imageView.setImageBitmap(imageBitmap);
			}
		});
		if(bitmap != null)
			imageView.setImageBitmap(bitmap);
	}
	
}
